//
//  UIWebViewController.m
//  UITest
//
//  Created by wp w on 12-6-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIWebViewController.h"
#import "DLog.h"
@implementation UIWebViewController

UIActivityIndicatorView *activity;
- (void)viewDidLoad {
	
	[super viewDidLoad];
	
	[self.view setBackgroundColor:[UIColor blackColor]];

	
	CGRect webRect = [UIScreen mainScreen].bounds;//CGRectMake(10,10,300,380);
	UIWebView *myWebView = [[UIWebView alloc] initWithFrame:webRect];
	myWebView.scalesPageToFit = YES;
	myWebView.delegate = self;
	
	NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    LogUrl(@"url:", url);
	NSURLRequest *request = [NSURLRequest requestWithURL:url];
	[myWebView loadRequest:request];
	
	[self.view addSubview:myWebView];
	
	activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
	[activity setCenter:CGPointMake(160,420)];
	[self.view addSubview:activity];	
	
	[myWebView release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - UIWebViewDelegate
//webview载入过程
//1.将要载入
//2.开始载入
//3.载入完成/失败
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    LogFun();
    //如果返回no，则不载入该url
    //返回yes，在该页面载入url
    return YES;
    
    //如果使用下边的代码，将打开系统浏览器，然后载入该rul
    /*NSURL *pageURL = [request URL];
    if ( ([[pageURL scheme] isEqualToString: @"http"]) && (navigationType == UIWebViewNavigationTypeLinkClicked ))
    {
        [[UIApplication sharedApplication] openURL:pageURL];
        return NO;
    }*/
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    LogFun();
	[activity startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    LogFun();
	[activity stopAnimating];
	[webView stringByEvaluatingJavaScriptFromString:@"alert('Finished Loading!');"];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    LogFun();
	[activity stopAnimating];
	LogErr(@"Error:",error);
}

@end
